package com.springboot.blog.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot.blog.dto.LayResponse;
import com.springboot.blog.entity.Nav;
import com.springboot.blog.interfaces.ToolInterface;
import com.springboot.blog.service.INavService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
class AdminNavController {
    @Autowired
    INavService navService;
    String PATH = "admin/nav/";

    @RequiresPermissions("nav:list")
    @GetMapping(value = {"/admin/nav/index", "/admin/nav"})
    public String index() {
        return PATH + "index";
    }

    @RequiresPermissions("nav:list")
    @PostMapping(value = {"/admin/nav/tree"})
    @ResponseBody
    public LayResponse tree() {
        QueryWrapper<Nav> wrapper = new QueryWrapper<>();
        List<Nav> navs = navService.list(wrapper.lambda().orderByAsc(Nav::getSorts));
        return new LayResponse().success("成功!").data(navs).setCount(9L);
    }

    @RequiresPermissions("nav:delete")
    @PostMapping(value = {"/admin/nav/delete"})
    @ResponseBody
    public LayResponse delete(String id) {
        boolean result = navService.removeById(id);
        LayResponse layResponse = new LayResponse();
        if (result) {
            return layResponse.success();
        }
        return layResponse.error();
    }

    @RequiresPermissions("nav:delete")
    @PostMapping(value = {"/admin/nav/deleteAll"})
    @ResponseBody
    public LayResponse deleteAll(@RequestParam("ids[]") List<Integer> ids) {
        boolean result = navService.removeByIds(ids);
        LayResponse layResponse = new LayResponse();
        if (result) {
            return layResponse.success();
        }
        return layResponse.error();
    }

    @RequiresPermissions("nav:edit")
    @PostMapping(value = {"/admin/nav/ajax/{id}"})
    @ResponseBody
    public LayResponse ajax(Nav nav) {
        boolean b = navService.updateById(nav);
        LayResponse layResponse = new LayResponse();
        if (b) {
            return layResponse.success();
        }
        return layResponse.error();
    }

    @RequiresPermissions("nav:add")
    @GetMapping(value = {"/admin/nav/add"})
    public String add(Model model) {
        List<Nav> list = navService.list(new QueryWrapper<Nav>().lambda().orderByAsc(Nav::getSorts));
        List<Nav> list_sort = new ArrayList<>();
        unlimitedSort(list, list_sort, 0, 0);
        model.addAttribute("navs", list_sort);
        return PATH + "add";
    }

    @RequiresPermissions("nav:add")
    @PostMapping(value = {"/admin/nav/add"})
    @ResponseBody
    public LayResponse add(@Validated(ToolInterface.insert.class) Nav nav) {
        navService.save(nav);
        return new LayResponse().success();
    }

    private void unlimitedSort(List<Nav> list, List<Nav> list_sort, int pid, int level) {
        for (Nav item : list) {
            if (item.getPid() == pid) {
                item.setTitle(StringUtils.repeat("　　", level) + "├" + item.getTitle());
                list_sort.add(item);
                unlimitedSort(list, list_sort, item.getId(), level + 1);
            }
        }
    }

    @RequiresPermissions("nav:edit")
    @GetMapping(value = {"/admin/nav/edit"})
    public String edit(Model model, @RequestParam Integer id) {
        List<Nav> list = navService.list(new QueryWrapper<Nav>().lambda().orderByAsc(Nav::getSorts));
        List<Nav> list_sort = new ArrayList<>();
        unlimitedSort(list, list_sort, 0, 0);
        Nav nav = navService.getById(id);

        model.addAttribute("navs", list_sort);
        model.addAttribute("nav", nav);
        return PATH + "edit";
    }

    @RequiresPermissions("nav:edit")
    @PostMapping(value = {"/admin/nav/edit"})
    @ResponseBody
    public LayResponse edit(@Validated(ToolInterface.update.class) Nav nav) {
        navService.updateById(nav);
        return new LayResponse().success();
    }
}
